import { Component, OnInit, Output, EventEmitter } from '@angular/core';
import { RemoveTableService } from '../../../../public/serve/remove-table/remove-table.service';
import { HintsService } from '../../../../public/serve/hints/hints.service';
import { internal } from '../../../internal.data';
import { Service } from 'app/server/server';
import { CudTableService } from 'app/public/serve/cud-table/cud-table.service';
declare var $:any;
@Component({
  selector: 'app-time-attribute-delete',
  templateUrl: './time-attribute-delete.component.html',
  styleUrls: ['./time-attribute-delete.component.css']
})
export class TimeAttributeDeleteComponent implements OnInit {

  @Output() onSubmit = new EventEmitter<void>();
  timeId:any;
  productId:any;
  constructor(
    public removeTableService: RemoveTableService,
    private hintsService: HintsService,
    private server: Service,
    public request: CudTableService,
  ) { }

  ngOnInit() {
  }

   /**
   * 详情
   * @param info
   */
  _details(id:any,productId){
    this.removeTableService.getRemoveTableId = [];
    this.removeTableService.keywordRemoveChose(true, id, 1)['bool'];
    this.timeId = id;
    this.productId = productId;
  }

  /**
   * 删除
   */
  _delete(){
    this.removeTableService
    .confirmRemove('ids',this.server.baseUrl +'Service/IdxProduct/deleteProductTimeMeta','token',internal.retrieval.token)
    .then(res => {
      
      if(res.result_code == '0000'){
        this.onSubmit.emit();
        this._saveProductFileContent(this.timeId);//删除左侧的时间属性
      }

      this.hintsService.messages(res.message,res.result_code);
      return;
    })
    .catch(err => {
      this.hintsService.messages('error','error');
      return;
    });
  }

  //保存内文加上标记后的内容
  _saveProductFileContent(timeId){
    let obj =  $('#internalContents').find('[class="indexing_a"][data-id='+timeId+']');
    obj.prop('outerHTML',obj.html());
    let internalContent = $('#internalContents').html();
 
    if(this.productId != '' && internalContent != ''){
       let data = {};
       data['token'] = internal.retrieval.token;
       data['productId'] = this.productId;
       data['FileContent'] = internalContent;
       let url = this.server.baseUrl + 'Service/IdxProduct/saveProductFileContent';
       this.request
         .postRequest(url, data)
         .then(res => {
         })
         .catch(err => {
             this.hintsService.messages("error", "error");
             return;
         });
    }
 }

}
